package com.ebring.shareEducation.service.impl;

import com.ebring.shareEducation.bean.*;
import com.ebring.shareEducation.bean.response.GoldCoinScore;
import com.ebring.shareEducation.dao.*;
import com.ebring.shareEducation.service.UserReviewsService;
import com.ebring.shareEducation.utils.RspMsg;
import com.ebring.shareEducation.utils.UuidUtils;
import com.ebring.shareEducation.bean.ScoreOrder;
import com.ebring.shareEducation.bean.Taskrelease;
import com.ebring.shareEducation.bean.UserReviews;
import com.ebring.shareEducation.dao.ScoreOrderMapper;
import com.ebring.shareEducation.dao.TaskreleaseMapper;
import com.ebring.shareEducation.dao.UserReviewsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserReviewsServiceImpl  implements UserReviewsService {
    @Autowired
    private UserReviewsMapper userReviewsMapper;

    @Autowired
    private TaskreleaseMapper taskreleaseMapper;

    @Autowired
    private ScoreOrderMapper scoreOrderMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public RspMsg<UserReviews> selectUserReviews(UserReviews userReviews) {
        RspMsg rspMsg = new RspMsg();
        Map<String,Object> map  = new HashMap<>();
        Example example = new Example(UserReviews.class);
        example.createCriteria().andEqualTo("userId",userReviews.getUserId());
        List<UserReviews> userReviewsList = userReviewsMapper.selectByExample(example);
        if (userReviewsList.size()>0){
            map.put("result",userReviewsList);
            rspMsg.setOK();
            rspMsg.setResult(map);
        }else {
            rspMsg.setError();
            rspMsg.setMsg("暂无评论");
        }

        return  rspMsg;

    }


    @Override
    public RspMsg<UserReviews> selectTaskReviews(UserReviews userReviews) {
        RspMsg rspMsg = new RspMsg();
        Map<String,Object> map  = new HashMap<>();
        Example example = new Example(UserReviews.class);
        example.createCriteria().andEqualTo("taskReleaseId",userReviews.getTaskReleaseId());
        List<UserReviews> userReviewsList = userReviewsMapper.selectByExample(example);
        if (userReviewsList.size()>0){
            map.put("result",userReviewsList);
            rspMsg.setOK();
            rspMsg.setResult(map);
        }else {
            rspMsg.setError();
            rspMsg.setMsg("暂无评论");
        }

        return  rspMsg;

    }

    @Override
    public List<UserReviews> selectTaskReviewe(UserReviews userReviews) {
        RspMsg rspMsg = new RspMsg();
        Map<String,Object> map  = new HashMap<>();
        Example example = new Example(UserReviews.class);
        example.createCriteria().andEqualTo("taskReleaseId",userReviews.getTaskReleaseId()).andEqualTo("role",userReviews.getRole());
        example.createCriteria().andEqualTo("taskReleaseId",userReviews.getTaskReleaseId());
        List<UserReviews> userReviewsList = userReviewsMapper.selectByExample(example);


        return  userReviewsList;

    }

    /**
     * 评论
     * @param userReviews
     * @return
     */
    @Override
    @Transactional
    public RspMsg<UserReviews> insertUserReviews(UserReviews userReviews ,String vipType) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = formatter.format(new Date());//格式化数据
        RspMsg rspMsg = new RspMsg();
        userReviews.setTId(UuidUtils.getNumberId(10));
        Example example = new Example(UserReviews.class);
        example.createCriteria().andEqualTo("taskReleaseId",userReviews.getTaskReleaseId());
        Taskrelease userReviewsList = taskreleaseMapper.selectByExample(example).get(0);
        //返积分
        ScoreOrder scoreOrder = new ScoreOrder();
        scoreOrder.setScoreId(UuidUtils.getNumberId(10));
        scoreOrder.setTradeTime(date);
        scoreOrder.setGetType("3");//获取或消费渠道 1个人信息完善 2签到 3评论
        scoreOrder.setPlusOrMinus("1");//正负 0负 1正
        User user;
        if(userReviewsList!=null) {
            //01发布人评论老师  02 老师评论发布人
            if (userReviews.getRole().equals("01")){
                 user = userMapper.selectByPrimaryKey(new User().setUserId(userReviewsList.getUserId()));
                //发布任务人评论接单人
                userReviews.setUserId(userReviewsList.getUserId());
                userReviews.setReviewId(userReviewsList.getRemark2());
                userReviewsList.setTaskStatus("10");//发布人评论老师
                scoreOrder.setUserId(userReviewsList.getUserId());
            }else {
                user = userMapper.selectByPrimaryKey(new User().setUserId(userReviewsList.getRemark2()));
                //接单人评论发布人
                userReviews.setUserId(userReviewsList.getRemark2());
                userReviews.setReviewId(userReviewsList.getUserId());
                userReviewsList.setTaskorderStatus("10");//老师评论家长
                scoreOrder.setUserId(userReviewsList.getRemark2());
            }
            //积分
            int score = 1;
            if (user.getScore()==null){
                if (vipType.equals("1")){//普通会员
                    score=score*2;

                }else if (vipType.equals("2")){//超级会员
                    score=score*3;
                }else{//普通用户
                    score=score;
                }
            }else {

                if (vipType.equals("1")){//普通会员
                    score = score*2;
                }else if (vipType.equals("2")){//超级会员
                    score = score*3;
                }else{//普通用户
                    score = Integer.parseInt(user.getScore())+score;
                }
            }
            scoreOrder.setAmount(score+"");
            user.setScore(Integer.parseInt(user.getScore())+score+"");

            userReviews.setReviewTime(date);//评论时间
            userMapper.updateByPrimaryKeySelective(user);//积分总数量修改
            scoreOrderMapper.insertSelective(scoreOrder);//积分流水记录
            int insert = userReviewsMapper.insertSelective(userReviews);
            if (insert > 0) {
                rspMsg.setOK();
                rspMsg.setMsg("评论成功,积分+"+score);

                taskreleaseMapper.updateByPrimaryKeySelective(userReviewsList);
            } else {
                rspMsg.setError();
                rspMsg.setMsg("评论失败");
            }
        }else{
            rspMsg.setError();
            rspMsg.setMsg("评论失败");
        }
        return  rspMsg;
    }

    /**
     * 用户签到
     * @param user
     * @return
     */
    @Override
    public RspMsg<User> UserSign(User user,String vipType) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String date = formatter.format(new Date());//格式化数据
        //积分流水
        ScoreOrder scoreOrder = new ScoreOrder();
        scoreOrder.setScoreId(UuidUtils.getNumberId(10));
        scoreOrder.setTradeTime(date);
        scoreOrder.setGetType("2");//获取或消费渠道 1个人信息完善 2签到 3评论
        scoreOrder.setPlusOrMinus("1");//正负 0负 1正
        scoreOrder.setUserId(user.getUserId());
        RspMsg rspMsg = new RspMsg();
        User user1 = userMapper.selectByPrimaryKey(user);
        int count =0;
        int scoreCount =1;//总积分
        if(user1.getSignTime()==null && user1.getScore()==null ){
            //用户首次签到
            count= count+1;
            user1.setSignTime(date);
            user1.setSignCount(count+"");
            if ("1".equals(vipType)){       //普通会员
                scoreCount=scoreCount*2;
            }else if ("2".equals(vipType)){ //超级会员
                scoreCount=scoreCount*3;
            }else {                         //非会员
                scoreCount=scoreCount;
            }
            user1.setScore(scoreCount+"");
            rspMsg.setOK();
            rspMsg.setMsg("签到成功");
            rspMsg.setResult(user1);
        }else if (date.equals(user1.getSignTime())){
            //当天已签到
            rspMsg.setError();
            rspMsg.setMsg("已签到");
        }else {
            //当天首次签到
            user1.setSignTime(date);
            user1.setSignCount(Integer.parseInt(user1.getSignCount())+1 +"");
            if ("1".equals(vipType)){//普通会员
                scoreCount=scoreCount*2;
            }else if ("2".equals(vipType)){//超级会员
                scoreCount=scoreCount*3;
            }else {
                scoreCount=scoreCount;
            }
            user1.setScore(Integer.parseInt(user1.getScore())+scoreCount+"");
            rspMsg.setOK();
            rspMsg.setMsg("已签到");
            rspMsg.setResult(user1);
        }
        scoreOrder.setAmount(scoreCount+"");
        scoreOrderMapper.insertSelective(scoreOrder);
        userMapper.updateByPrimaryKeySelective(user1);
        return rspMsg;
    }


    @Override
    public RspMsg<UserReviews> SelectReviewsGrade(String type ,String userId ,String role) {
        Map<String,Object> map  = new HashMap<>();
        RspMsg rspMsg = new RspMsg();
        Example example = new Example(UserReviews.class);
        if (type.equals("01")){
            example.or().andBetween("avg",new String("1"),new String("2")).andEqualTo("userId",userId).andEqualTo("role",role);//差评
        }else if (type.equals("02")){
            example.or().andBetween("avg",new String("3"),new String("10")).andEqualTo("userId",userId).andEqualTo("role",role);//中评
        }else if(type.equals("03")){
            example.or().andBetween("avg",new String("3"),new String("6")).andEqualTo("userId",userId).andEqualTo("role",role);//好评
        }else{

        }
        List<UserReviews> userReviewsList = userReviewsMapper.selectByExample(example);
        map.put("userReviewsList",userReviewsList);
        System.out.println(userReviewsList);
        rspMsg.setOK();
        rspMsg.setResult(map);
        return rspMsg;
    }

    @Override
    public RspMsg<GoldCoinScore> SelectUserSign(String userId) {
        RspMsg rspMsg = new RspMsg();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String date = formatter.format(new Date());//格式化数据
        GoldCoinScore goldCoinScore = new GoldCoinScore();
        User user = new User();
        user.setUserId(userId);
        User user1 = userMapper.selectByPrimaryKey(user);
        if (user1.getScore()==null){
            goldCoinScore.setScore("0");
        }else {
            goldCoinScore.setScore(user1.getScore());
        }
        if (user1.getPayGold()==null){
            goldCoinScore.setPayGold("0");
        }else {
            goldCoinScore.setPayGold(user1.getPayGold());
        }
        if(date.equals(user1.getSignTime())){
            goldCoinScore.setState("1");//已签到
        }else{
            goldCoinScore.setState("0");
        }
        rspMsg.setOK();
        rspMsg.setResult(goldCoinScore);
        return rspMsg;
    }

}
